<template>
  <div class="youhuiqian">
    <div :class="hidd ? 'from' : 'from-active'">
      <el-button v-if="show" @click="changeHidd">添加新优惠券</el-button>
      <div style="margin-top: 20px;">
        <el-table :data="tableData ? tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize) : [].slice((currentPage - 1) * pageSize, currentPage * pageSize)" border style="width: 100%">
          <el-table-column prop="id" label="优惠券ID" width="180">
          </el-table-column>
          <el-table-column prop="date" :formatter="formatTime" label="发放时间" width="180">
          </el-table-column>
          <el-table-column prop="afdate"  label="结束时间" width="180">
          </el-table-column>
          <el-table-column prop="number" label="发放数量" width="180"></el-table-column>
          <el-table-column label="使用条件" width="180">
            <template #default="scope">
              {{ scope.row.vips}}
            </template>
          </el-table-column>
          <el-table-column label="优惠项目" width="180">
            <template #default="scope">
              {{scope.row.select=='无门槛券'?'立减'+scope.row.jian: '满' + scope.row.man + '-' + scope.row.jian }}
            </template>
          </el-table-column>
          <el-table-column prop="shengyu" label="剩余数量" ></el-table-column>
        </el-table>
      </div>
    </div>
    <div :class="!hidd ? 'add' : 'add-active'">
      <div @click="changeHidd" class="back">
        <i style="font-size: 20px;" class="el-icon-arrow-left"></i>
      </div>
      <div class="add-content">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="排序">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label="优惠券名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="分类">
            <el-select @change="changeSelect" v-model="form.select" placeholder="请选择优惠券分类">
              <el-option v-for="(item, index) in selectList" :label="item.name" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用条件">
            <el-input :disabled="showIpt" type="number" v-model="form.man"></el-input>
          </el-form-item>
          <el-form-item label="使用条件">
            <el-input @input="changeTime" type="number" placeholder="" v-model="form.timeday">
              <template slot="prepend">
                <el-radio v-model="radio" label="获得后"></el-radio>
              </template>
              <template slot="append">（天）内有效</template>
            </el-input>
          </el-form-item>
          <el-form-item label="优惠方式">
            <el-input type="number" placeholder="" v-model="form.jian">
              <template slot="prepend">
                立减
              </template>
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="发送张数">
            <el-input @input="changeSheng" type="number" placeholder="" v-model="form.number"></el-input>
          </el-form-item>
          <el-form-item label="是否限制会员等级">
            <el-radio v-model="radio2" label="是"></el-radio>
            <el-radio v-model="radio2" label="否"></el-radio>
            <div class="show-border" v-if="radio2 == '是'">
              <div> 会员等级： </div>
              <div>
                <el-checkbox-group @change="radios" v-model="checkList">
                  <el-checkbox label="普通等级"></el-checkbox>
                  <el-checkbox label="lv1"></el-checkbox>
                  <el-checkbox label="lv2"></el-checkbox>
                  <el-checkbox label="lv3"></el-checkbox>
                  <el-checkbox label="lv4"></el-checkbox>
                  <el-checkbox label="lv5"></el-checkbox>
                </el-checkbox-group>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="是否限制注册时间">
            <el-radio v-model="radio3" label="是"></el-radio>
            <el-radio v-model="radio3" label="否"></el-radio>
            <div class="show-border" v-if="radio3 == '是'">
              <div>
                <el-checkbox-group @change="radioss" v-model="checkList2">
                  <el-checkbox :label="form.input1">
                    <el-input type="number" placeholder="" v-model="form.input1">
                      <template slot="prepend">注册时间在</template>
                      <template slot="append">（天）内的用户可以领取</template>
                    </el-input>
                  </el-checkbox>
                  <br>
                  <el-checkbox :label="form.input2">
                    <el-input type="number" placeholder="" v-model="form.input2">
                      <template slot="prepend">注册时间在</template>
                      <template slot="append">（天）上的用户可以领取</template>
                    </el-input>
                  </el-checkbox><br>
                  <el-checkbox label="24小时">注册时间在24小时内的用户可以领取</el-checkbox>
                </el-checkbox-group>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="商品使用限制">
            <el-radio v-model="radio4" label="是"></el-radio>
            <el-radio v-model="radio4" label="否"></el-radio>
            <el-button v-if="radio4 == '是'" @click="setFlag">选择商品</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">确定</el-button>
            <el-button @click="changeHidd">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-pagination v-if="hidd" @size-change="handleSizeChange2" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-size="pageSize" :total="tableData.length ? tableData.length : 0" />
    <div class="zz" v-show="flag">
      <div class="set">
        <!-- 头部 -->
        <div class="setTop">
          <p style="color: #fff;">选择商品</p>
          <i class="el-icon-close" style="font-size: 20px;font-weight: bold;color: #fff;" @click="setFlag"></i>
        </div>
        <div>
          <!-- 主体 -->
          <div class="content">
            <table>
              <!-- 表头 -->
              <th>1</th>
              <th>2</th>
              <th>3</th>
              <th>操作</th>
              <!-- 每一项 -->
              <tr v-for="(item, index) in shopList">
                <td><img width="50px" :src="item.img" alt=""></td>
                <td>￥{{ item.pice }}</td>
                <td>{{ item.mSale }}</td>
                <td>
                  <button @click="setFlag">选择</button>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import { getZwyShopListBx,addYhMingxiDjm ,getSelectListDjm} from '../../utils/api'
export default {
  data() {
    return {
      tableData2: [],
      hidd: true,
      selectList: [],
      radio: '获得后',
      radio2: '是',
      radio3: '是',
      radio4: '是',
      showIpt:false,
      currentPage: 1,
      pageSize: 7,
      form: {
        date:new Date(),
        afdate:new Date(),
        name: '',
        select: '',
        man: '',
        timeday: '',
        jian: '',
        number: '',
        vips: '',
        tts: '',
        vip:true,
        tt:true,
        shengyu:'',
        timer:true,
        shops:'',
        shop:true
      },
      checkList2: [],
      checkList: [],
      flag: false,
      shopList: []
    }
  },
  created() {
    this.render()
    getSelectListDjm().then(res=>{
      this.selectList=res.data.data
    })
  },
  methods: {
    render() {
      getZwyShopListBx().then(res => {
      this.shopList = res.data.data
    })
    },
    handleSizeChange2(val) {
      this.pageSize = val;
      this.currentPage = 1;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    changeSheng(){
      this.form.shengyu=this.form.number
    },
    formatTime(row, column) {
      let date = row[column.property];
      if (date === undefined) {
        return ''
      }
      return moment(date,moment.ISO_8601).format("YYYY-MM-DD")
      
    },
    changeSelect(){
      if(this.form.select=='无门槛券'){
        this.form.man=0
        this.showIpt=true
      }else{
        this.form.man=null
        this.showIpt=false
      }
    },
    changeTime(){
      let af=(this.form.timeday*86400)+Math.round(new Date().getTime()/1000)
      let date = new Date(af * 1000);
      let year=date.getFullYear()+'-'
      let M=(date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
      let D=date.getDate()+' '
      this.form.afdate=year+M+D
    },
    changeHidd() {
      this.hidd = !this.hidd
    },
    onSubmit() {
      addYhMingxiDjm(this.form).then(res=>{
        this.hidd = !this.hidd
        this.render()
      })
    },
    setFlag() {
      this.flag = !this.flag
    },
    radios() {
      let str=''
      this.checkList.map(item=>{
        str+=item
      })
      this.form.vips=str
    },
    radioss() {
    }
  },
  props: {
    tableData: {
      type: Array
    },
    show: {
      type: Boolean
    }
  }
}
</script>

<style scoped>
.youhuiqian {
  width: 100%;
  overflow: hidden;
}

.from {
  display: block;
}

.from-active {
  display: none;
}

.add {
  display: block;
  overflow: hidden;
}

.add-active {
  display: none;
  width: 100%;
}

.back {
  width: 100%;
}

.add-content {
  width: 100%;
  margin-top: 20px;
}

.show-border {
  width: 80%;
  padding: 5px 10px;
  border: 2px solid #ccc;
  display: flex;
}

.zz {
  z-index: 10000;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.setTop {
  padding: 18px 22px;
  display: flex;
  justify-content: space-between;
  background-color: #0790F5;
}

.set {
  font-size: 14px;
  width: 50%;
  height: 400px;
  position: absolute;
  top: 50%;
  left: 60%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background: #ffffff;
  border-radius: 5px;
}

.content {
  width: 85%;
  margin: 40px auto;
  height: 100%;
}

.lin2 button {
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  background-color: #fff;
  border-radius: 5px;
  width: 50px;
  height: 30px;
}

.el-upload__tip {
  color: red;
}

.lin2 {
  display: flex;
  width: 40%;
  margin: 0 auto;
  justify-content: space-around;
}

.upload-demo {
  margin-left: 20px;
  margin-top: -6px;
}

.nameipt {
  width: 100px;
  border: none;
  padding: 10px;
  margin-left: 60px;
  margin-top: -10px;
}

.icon1 {
  width: 25px;
  margin-left: 40px;
  margin-top: -5px;
}

.lin1 {
  display: flex;
  margin-bottom: 30px;
}

.nr {
  width: 100%;
  height: 100%;
  font-size: 14px;
}

.icon {
  width: 30px;
}

.tab {
  padding: 20px 40px;
  display: flex;
}

.content {
  width: 85%;
  margin: 40px auto;
  height: 100%;
}

.tabbtn {
  margin-left: 10px;
  margin-right: 10px;
  border: none;
  color: #0790F5;
  font-weight: bold;
  background-color: #fff;
}

table {
  margin-top: 30px;
  border: 1px solid #777777;
  border-collapse: collapse;
  text-align: center;
  width: 100%;
  font-size: 14px;
}

tr {
  height: 50px;
}

td {
  width: 150px;
  border: 1px solid #777777;
}

th {
  height: 50px;
  line-height: 50px;
  font-weight: normal;
  background-color: #F6F6F6;
  border: 1px solid #777777;
}</style>